package tuan.score;

public class NumDirImgUrlScore extends ImgUrlScore {

	public NumDirImgUrlScore(double score) throws Exception {
		super(score);
	}

	protected double getUrlScore(String url) {
		if (url.indexOf("?") >= 0) {
			url = url.substring(0, url.indexOf("?"));
			if (url.lastIndexOf(".") >= 0) {
				String suffix = url.substring(url.lastIndexOf(".") + 1);
				url = url.substring(0, url.lastIndexOf("."));
				if (!suffixSet.contains(suffix)) {
					return score;
				}
			}
		}
		// 去除域名
		if (url.indexOf("http://") >= 0) {
			url = url.substring(7);
			if (url.indexOf("/") > 0) {
				url = url.substring(url.indexOf("/"));
			}
		}

		//数字+md5识别
		String[] dirs = url.split("/");
		for (String dir : dirs) {
			if (isNum(dir)) {
				return score;
			}
			if (dir.length() >= 20) {
				return score;
			}
		}
		return 0.0;
	}

	/**
	 * 判断子串是否是纯10进制数字
	 * 
	 * @param str
	 * @return
	 */
	boolean isNum(String str) {
		if (str.length() == 0) {
			return false;
		}
		str = str.toLowerCase();
		for (int i = str.length(); --i >= 0;) {
			char c = str.charAt(i);
			if ((c < '0' || c > '9') && (c != '-' || c != '_')) {
				return false;
			}
		}
		return true;
	}
}
